$(function() {
	var marks = $('#marks').val();
	
	$('#tt').edatagrid({
		onClickRow : function(index, row) {
			 if(checkEnding()){
				 editIndex = index;
	         }
			if (row.loginType != 1 && row.loginType != 13) {
				$('#tt').datagrid('selectRow',index).datagrid('beginEdit',index)
				endEditing(index, 'payRate');
				endEditing(index, 'payMin');
				endEditing(index, 'payMax');
			}
		},
	/*
	 * onAfterEdit : function(rowIndex, rowData, changes) { var
	 * r=confirm("你确定本次修改吗？"); if (r==true) { $.ajax({ url :
	 * '/apkconfig/batchSaveLoginType', type : 'POST', data : { marks: marks,
	 * rate: rowData.rate, loginType: rowData.loginType, apkId: rowData.apkId,
	 * base: rowData.base, loginName: rowData.loginName, leftNum:
	 * rowData.leftNum, execTotal: rowData.execTotal, payRate: rowData.payRate,
	 * payMin: rowData.payMin, payMax: rowData.payMax, bizId: rowData.bizId,
	 * mobile: rowData.mobile } }); } },
	 */
		onCancelEdit:function(index,row){
			row.editing = false;
			$('#tt').datagrid('updateRow',{
				index: index,
				row:{}
			});
		}
	})
	
	 var editIndex = undefined;
	 function checkEnding(){
         if(editIndex == undefined) {
        	 return true;
         }
         if($('#tt').datagrid('validateRow',editIndex)) {
             $('#tt').datagrid('endEdit',editIndex);
             editIndex = undefined; 
             return true;
          }else{
        	  return false;
          }
      }

	
	function endEditing(index, field){
		var cellEdit = $('#tt').datagrid('getEditor', {
			index : index,
			field : field
		});
		var $input = cellEdit.target; 
		$input.attr("disabled", true);
	}

	// 输入框数字限制，最大为100且只最多能有两位小数点
	$(document).on(
			"keyup afterpaste",
			"td[field='rate'] input",
			function(e) {
				this.value = this.value.replace(/[^\d.]/g, ""); // 清除“数字”和“.”以外的字符
				this.value = this.value.replace(/\.{2,}/g, "."); // 只保留第一个.
																	// 清除多余的
				this.value = this.value.replace(".", "$#$").replace(/\./g, "")
						.replace("$#$", ".");
				this.value = this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,
						'$1$2.$3');// 只能输入两个小数
				if (this.value.indexOf(".") < 0 && this.value != "") {// 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于
																		// 01、02的金额
					this.value = parseFloat(this.value);
				}
				if (this.value > 100) {
					this.value = 100;
				}
			})

});

function formatterLoginType(value, row, index) {
	return row.loginName;
}

function batchEditLogin(){
	var arr = new Array();
    var updateObj;
	var rows = $("#tt").datagrid('getRows');
	for(var i=0;i<rows.length;i++){
		if (rows[i].rate > 0) {
		  updateObj = new Object();
		  updateObj.rate=rows[i].rate;
		  updateObj.loginType= rows[i].loginType;
		  updateObj.apkId= rows[i].apkId;
		  updateObj.base=rows[i].base;
		  updateObj.loginName= rows[i].loginName;
		  updateObj.leftNum=rows[i].leftNum;
		  updateObj.execTotal=rows[i].execTotal;
		  updateObj.payRate= rows[i].payRate;
		  updateObj.payMin= rows[i].payMin;
		  updateObj.payMax= rows[i].payMax;
		  updateObj.bizId=rows[i].bizId;
		  updateObj.mobile= rows[i].mobile;
		  arr.push(updateObj);
		}
	}
	var list=JSON.stringify(arr);
	var marks = $('#marks').val();
	console.info(marks);
	$.messager.confirm('提示', '你确定本次操作吗', function(opt){
		if (opt){
			$.ajax({
				url : '/apkconfig/batchSaveLoginTypeNew',
				type:'POST',
				datatype:'json',
				data:{'list' : list, 'marks' : marks},
				success: function(data) {
					alert("操作成功！")
				}
			})
		}
	});	
	
}